package com.vgmobile.cucrever2.global;

import java.io.File;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ImageView;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.vgmobile.cucrever2.R;

public class BaseActivity extends FragmentActivity{

	public static final String TAG_FRAGMENT_HOME = "Home";
//	public static final String TAG_FRAGMENT_HOME_TO_CATPARENT = "HomeToCategoryParent";
//	public static final String TAG_FRAGMENT_CATPARENT_TO_CATCHILD = "CategoryParentToCategoryChild";
//	public static final String TAG_FRAGMENT_XXX_TO_ORDER = "XXXToOrder";
	
	public static final int CODE_SCAN = 1111;
	
	private static DisplayImageOptions options;
	public static ImageLoader imageLoader;
	
	protected BaseActivity activity;
	protected CucreApplication application;
	
	private OnBackPressedListener onBackPressedListener;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		activity = this;
		application = (CucreApplication) getApplication();
	}
	
	public void setupImageLoad(){
		File cacheDir = StorageUtils.getCacheDirectory(this);
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
																		.discCacheExtraOptions(500, 500, CompressFormat.JPEG, 50, null)
																        .memoryCacheSize(2 * 1024 * 1024)
																        .discCache(new UnlimitedDiscCache(cacheDir))
																        .discCacheSize(20 * 1024 * 1024)
																        .discCacheFileCount(100)
																        .build();
		ImageLoader.getInstance().init(config);
		options = new DisplayImageOptions.Builder()
						.showImageOnLoading(R.drawable.null_stubload)
						.showImageForEmptyUri(R.drawable.null_stubload)
						.showImageOnFail(R.drawable.null_stubload)
						.cacheOnDisc(true)
						.cacheInMemory(true)
						.bitmapConfig(Bitmap.Config.ALPHA_8)
						.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
						.build();
		imageLoader = ImageLoader.getInstance();
	}
	
	public void loadImage(ImageView imageView, String linkImage){
		imageLoader.displayImage(linkImage, imageView, options);
	}
	
	public void setOnBackPressedListener(OnBackPressedListener onBackPressedListener) {
	    this.onBackPressedListener = onBackPressedListener;
	}
	
	@Override
	public void onBackPressed() {
	    if (onBackPressedListener != null){
	        onBackPressedListener.doBack();
	    }else{
	        super.onBackPressed();
	        overridePendingTransition(R.anim.activity_back_fade_in, R.anim.activity_back_fade_out);
	    }
	}

	public void clickBack(View view){
		onBackPressed();
	}
	
	public void clickCart(View view){
		
	}
}
